﻿using System;
using Grundy.Common;
using Grundy.Entity;
using Grundy.Message;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FluentAssertions;

namespace Grundy.Licensing.Server.Tests
{
    [TestClass]
    public class LicenseEntitlementValidityTests
    {
        [TestMethod]
        public void Should_be_valid_for_past_from_no_to()
        {
            var le = GetCle(new LicenseEntitlement {ValidFrom = Clock.Instance.Utc.AddDays(-1)});
            var lev = new LicenseEntitlementValidity(le);
            Assert.IsTrue(lev.IsValid());
        }

        [TestMethod]
        public void Should_be_valid_for_past_from_future_to()
        {
            var le = GetCle(new LicenseEntitlement { ValidFrom = Clock.Instance.Utc.AddDays(-1), ValidTo = Clock.Instance.Utc.AddDays(1)});
            var lev = new LicenseEntitlementValidity(le);
            Assert.IsTrue(lev.IsValid());
        }

        [TestMethod]
        public void Should_be_be_valid_for_past_from_and_to_today()
        {
            var le = GetCle(new LicenseEntitlement { ValidFrom = Clock.Instance.Utc.AddDays(-1), ValidTo = Clock.Instance.Utc });
            var lev = new LicenseEntitlementValidity(le);
            Assert.IsTrue(lev.IsValid());
        }

        [TestMethod]
        public void Should_be_valid_for_today_from_and_to_today()
        {
            var le = GetCle(new LicenseEntitlement { ValidFrom = Clock.Instance.Utc, ValidTo = Clock.Instance.Utc });
            var lev = new LicenseEntitlementValidity(le);
            Assert.IsTrue(lev.IsValid());
        }

        [TestMethod]
        public void Should_not_be_valid_for_tomorrow_from_and_no_to()
        {
            var le = GetCle(new LicenseEntitlement { ValidFrom = Clock.Instance.Utc.AddDays(1) });
            var lev = new LicenseEntitlementValidity(le);
            Assert.IsFalse(lev.IsValid());
        }

        [TestMethod]
        public void Should_be_valid_for_no_from_and_no_to()
        {
            var le = GetCle(new LicenseEntitlement {});
            var lev = new LicenseEntitlementValidity(le);
            Assert.IsTrue(lev.IsValid());
        }

        [TestMethod]
        public void Should_not_be_valid_for_no_from_and_past_to()
        {
            var le = GetCle(new LicenseEntitlement { ValidTo = Clock.Instance.Utc.AddDays(-1) });
            var lev = new LicenseEntitlementValidity(le);
            Assert.IsFalse(lev.IsValid());
        }

        [TestMethod]
        public void Should_not_be_valid_for_past_from_and_past_to()
        {
            var le = GetCle(new LicenseEntitlement { ValidFrom = Clock.Instance.Utc.AddDays(-2), ValidTo = Clock.Instance.Utc.AddDays(-1) });
            var lev = new LicenseEntitlementValidity(le);
            Assert.IsFalse(lev.IsValid());
        }

        [TestMethod]
        public void Should_not_be_valid_for_future_from_and_future_to()
        {
            var le = GetCle(new LicenseEntitlement { ValidFrom = Clock.Instance.Utc.AddDays(1), ValidTo = Clock.Instance.Utc.AddDays(2) });
            var lev = new LicenseEntitlementValidity(le);
            Assert.IsFalse(lev.IsValid());
        }

        [TestMethod]
        public void Should_override_to_string()
        {
            var le = GetCle(new LicenseEntitlement { ValidFrom = Clock.Instance.Utc.AddDays(1), ValidTo = Clock.Instance.Utc.AddDays(2) });
            var lev = new LicenseEntitlementValidity(le);
            lev.ToString().Should().Contain("expired");
            le = GetCle(new LicenseEntitlement { ValidTo = Clock.Instance.Utc.AddDays(2) });
            lev = new LicenseEntitlementValidity(le);
            lev.ToString().Should().NotContain("expired");
        }

        private CustomerLicenseEntitlement GetCle(LicenseEntitlement le)
        {
            return new CustomerLicenseEntitlement(Guid.Empty, string.Empty, le, null);
        }
    }
}